<!--物料-->
<template>
  <div>
    <!-- --------------------------------搜索框----------------------------- -->
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input
            v-model="query.itemCode"
            placeholder="物料编码"
            :clearable="true"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="query.itemName"
            placeholder="物料名称"
            :clearable="true"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="query.itemType"
            placeholder="分类"
            :clearable="true"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="loadItem"
          >搜索
          </el-button
          >
          <el-button type="success" @click="dialog.addShow = true"
          >新建
          </el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- -----------------------------分页显示数据--------------------------------------- -->
    <el-table :data="itemList" border style="width: 100%">
      <el-table-column type="index" width="50" label="序号"></el-table-column>

      <el-table-column prop="itemCode" label="物料编码" width="150"></el-table-column>
      <el-table-column prop="itemName" label="物料名称" width="150"></el-table-column>
      <el-table-column prop="itemType" label="分类" width="150"></el-table-column>
      <el-table-column prop="unit" label="单位类别" width="150"></el-table-column>
      <el-table-column prop="expiryDate" label="有效期" width="150"></el-table-column>
      <el-table-column prop="remark" label="备注" width="150"></el-table-column>
      <el-table-column prop="delFlag" label="有效" width="150">
        <template slot-scope="scope">
          <el-tag :type="scope.row.delFlag === 0 ? 'success' : 'danger'">
            {{ scope.row.delFlag === 0 ? '有效' : '无效' }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="450">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="small"
            @click="handleEdit(scope.row.id)"
          >编辑
          </el-button
          >
          <el-button
            v-if="scope.row.delFlag === 0"
            type="warning"
            slot="reference"
            size="mini"
            icon="el-icon-warning"
            @click="handleQuit(scope.row.id,scope.row.delFlag)"
          >失效
          </el-button>
          <el-button
            v-else-if="scope.row.delFlag === 1"
            type="success"
            slot="reference"
            size="mini"
            icon="el-icon-warning"
            @click="handleQuit(scope.row.id,scope.row.delFlag)"
          >恢复
          </el-button>          <el-popconfirm
            title="这是一段内容确定删除吗？"
            @confirm="handleDelete(scope.row.id)"
          >
            <el-button
              type="danger"
              slot="reference"
              size="mini"
              icon="el-icon-delete"
            >删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.currentPage"
      :page-sizes="pageInfo.pageSizes"
      :page-size="query.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total"
    >
    </el-pagination>
    <!-- ------------------------------------添加操作------------------------------------ -->
    <el-dialog title="添加物料" :visible.sync="dialog.addShow" width="30%">
      <el-form ref="addForm" :model="addItem" :rules="rules">
        <el-form-item
          label="物料编码"
          :label-width="formLabelWidth"
          prop="itemCode"
        >
          <el-input v-model="addItem.itemCode" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="物料名称"
          :label-width="formLabelWidth"
          prop="itemName"
        >
          <el-input v-model="addItem.itemName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类" :label-width="formLabelWidth" prop="itemType">
          <el-input v-model="addItem.itemType" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="单位类别" :label-width="formLabelWidth" prop="unit">
          <el-input v-model="addItem.unit" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="有效期" :label-width="formLabelWidth" prop="expiryDate">
          <el-input v-model="addItem.expiryDate" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth" prop="remark">
          <el-input v-model="addItem.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog.addShow = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- ------------------------------------修改操作------------------------------------ -->
    <el-dialog title="修改物料" :visible.sync="dialog.updateShow" width="30%">
      <el-form ref="updateForm" :model="updateItem" :rules="rules">
        <el-form-item
          label="物料编码"
          :label-width="formLabelWidth"
          prop="itemCode"
        >
          <el-input v-model="updateItem.itemCode" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="物料名称"
          :label-width="formLabelWidth"
          prop="itemName"
        >
          <el-input v-model="updateItem.itemName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类" :label-width="formLabelWidth" prop="itemType">
          <el-input v-model="updateItem.itemType" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="单位类别" :label-width="formLabelWidth" prop="unit">
          <el-input v-model="updateItem.unit" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="有效期" :label-width="formLabelWidth" prop="expiryDate">
          <el-input v-model="updateItem.expiryDate" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth" prop="remark">
          <el-input v-model="updateItem.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog.updateShow = false">取 消</el-button>
        <el-button type="primary" @click="update()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "material.vue",
  data() {
    return {
      checkList: ['选中且禁用', '复选框 A'],
      formLabelWidth: "100px",
      addItem: {},
      updateItem: {},
      quitItem: {},
      rules: {
        itemName: [
          {required: true, message: "请输入物料名称", trigger: "blur"},
          {
            min: 2,
            max: 15,
            message: "长度在 2 到 15 个字符",
            trigger: "blur",
          },

        ],
        itemCode: [
          {required: true, message: "请输入编码", trigger: "blur"},
          {pattern: /[a-z]{1}\d*/, message: '请输入以一位大写英文字母开头的编码', trigger: 'blur'}
        ],
        expiryDate: [
          {required: true, message: "请输入日期", trigger: "blur"},
          {pattern: /^\d{4}-\d{2}-\d{2}$/, message: '请输入YYYY-MM-DD格式日期', trigger: 'blur'}
        ]
      },
      dialog: {
        addShow: false,
        updateShow: false,
        roleShow: false,
      },
      query: {
        itemCode: "",
        itemName: "",
        currentPage: 1, //当前页
        pageSize: 5, //每页显示的条数
      },
      pageInfo: {
        total: 0, //总条数
        pageSizes: [5, 10, 20, 50],
      },
      itemList: {}
    };
  },
  methods: {
    handleEdit(id) {
      this.$axios.get("/material/" + id).then((res) => {
        this.updateItem = res.data.data.item;
        this.dialog.updateShow = true; //显示模态框
      });
    },
    update() {
      this.$axios.put("/material/", this.updateItem).then((res) => {
        if (res.data.code == 200) {
          this.$message.success(res.data.message);
          this.dialog.updateShow = false; //关于模态框
          this.loadItem(); //重新加载数据
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    handleDelete(id) {
      this.$axios.delete("/material/" + id).then((res) => {
        if (res.data.code == 200) {
          this.$message.success(res.data.message);
          this.loadItem(); //重新加载数据
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    add() {
      this.$axios.post("/material/", this.addItem).then((res) => {
        if (res.data.code == 200) {
          this.$message.success(res.data.message);
          this.dialog.addShow = false; //关于模态框
          this.$refs.addForm.resetFields(); //清空表单数据
          this.loadItem(); //重新加载数据
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    handleSizeChange(val) {
      this.query.pageSize = val;
      this.loadItem();
    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.loadItem();
    },
    loadItem() {
      this.$axios.post("/material/queryAll", this.query).then((res) => {
        this.itemList = res.data.data.list;
        // this.supplierList = res.data.data.list.map((supplier) => {
        //   supplier.delFlag = supplier.delFlag === 0 ? '有效' : '禁用';
        //   return supplier;
        // });
        //返回当前页数据
        this.pageInfo.total = res.data.data.total; //返回一共有多少条数据
      });
    },
    handleQuit(id,delFlag) {
      this.$set(this.quitItem, 'id', id);
      delFlag = (delFlag === 0?1:0);
      this.$set(this.quitItem, 'delFlag', delFlag);
      this.$axios.put("/material/quit",this.quitItem).then((res)=>{
        if (res.data.code == 200) {
          this.$message.success(res.data.message);
          this.loadItem(); //重新加载数据
        } else {
          this.$message.error(res.data.message);
        }
      })
    }
  },
  mounted() {
    this.loadItem();
  },
}
</script>

<style scoped>

</style>

